package com.xcw.strategy.handlerService;



import com.google.common.collect.Maps;
import com.xcw.strategy.annotation.HandlerType;
import com.xcw.strategy.util.ClassScanner;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;


import java.util.Map;


/**
 * @Author bhx
 * @Date 2022/7/18 1:10
 * @Description TODO
 */
@Component
@SuppressWarnings("unchecked")
public class HandlerProcessor implements BeanFactoryPostProcessor {

    private static final String HANDLER_PACKAGE = "com.xcw.strategy.Handle";


    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        Map<String,Class> handerMap = Maps.newHashMapWithExpectedSize(3);
        ClassScanner.scan(HANDLER_PACKAGE, HandlerType.class).forEach(clazz ->{
            String type = clazz.getAnnotation(HandlerType.class).value();
            handerMap.put(type,clazz);
        });
        HandlerContext context = new HandlerContext(handerMap);
        beanFactory.registerSingleton(HandlerContext.class.getName(),context);
    }
}
